package com.shop.cereshop.app.dl.logic.demo;

import com.shop.cereshop.app.dao.buyer.CereBuyerWithdrawalDAO;
import com.shop.cereshop.app.dl.logic.service.ChangeService;
import com.shop.cereshop.app.dl.logic.utils.ListPageHelper;
import com.shop.cereshop.app.dl.logic.utils.ResultInfo;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.app.service.log.CerePlatformLogService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.buyer.CereBuyerWithdrawal;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @program: dlshop
 * 描述：
 * @author: 我好帅
 * @create: 2021-07-21 15:13
 **///提现到零钱
@RestController
@Slf4j(topic = "Change")
@RequestMapping("/withdrawToChange")
public class Change {
    @Value("${MinAmount}")
    private String MinAmount;
    @Value("${MaxAmount}")
    private String MaxAmount;

    @Autowired
    private ChangeService changeService;
    @Autowired
    private CereBuyerUserService cereBuyerUserService;
    @Autowired
    private CereBuyerWithdrawalDAO cereBuyerWithdrawalDAO;
    @Autowired
    private CerePlatformLogService cerePlatformLogService;


    @ApiOperation(value = "企业转账到零钱", httpMethod = "POST", produces = "application/json;charset=UTF-8")
    @ApiImplicitParams(value = {@ApiImplicitParam(value = "*用户token", name = "token", defaultValue = "", dataType = "String", paramType = "header"),
            @ApiImplicitParam(value = "金额", name = "money", defaultValue = "", dataType = "String", paramType = "query")
    })
    @PostMapping("/wxpay/transfer")
    public ResultInfo transfer(HttpServletRequest request, BigDecimal money, String openID) throws Exception {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        if(user == null){
            return ResultInfo.error("请先登录");
        }
        if (openID==null || openID.equals("")){
            openID = user.getWechatOpenId();
        }
        BigDecimal Min = new BigDecimal(MinAmount);
        BigDecimal Max = new BigDecimal(MaxAmount);
        //w判断传过来的钱是否是整数
        //todo
        boolean numeric = ListPageHelper.isNumeric(money + "");
        if (numeric) {
//            w最低提现金额为10元
            if (money.compareTo(Min) < 0) {
                return ResultInfo.error("提现金额需大于10");
                //w如果10<=money<=100, 就自动提现, 否则需要进行审核
            } else if (money.compareTo(Min) >= 0 && money.compareTo(Max) <= 0) {
                //w传过来的钱是元单位, 需要的参数是分单位
                money = money.multiply(new BigDecimal(100));
                changeService.transfer(request, money, openID,user);
            } else {
                //根据手机号查询分销员数据
                CereBuyerWithdrawal withdrawal = new CereBuyerWithdrawal();
                String time = TimeUtils.yyMMddHHmmss();
                withdrawal.setApplyTime(time);
                withdrawal.setState(IntegerEnum.BUYER_WITHDRAWAL_STAY.getCode());
                withdrawal.setBuyerUserId(user.getBuyerUserId());
                withdrawal.setBankName("提现到零钱");
                withdrawal.setBankCard("0");
                withdrawal.setWithdrawalMoney(money);
                withdrawal.setType(1);
                cereBuyerWithdrawalDAO.insert(withdrawal);
                //新增日志
                cerePlatformLogService.addLog(user, "账户模块", "客户端操作", "发起提现申请", withdrawal.getWithdrawalId(), time);
                return new ResultInfo();
            }
        }
        return ResultInfo.error("提现金额需为整数");
    }
}
